/* Alice Robinson and Holly Beach
 * TCSS491 - Winter 2012*/

import java.io.Serializable;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;


/**A PTriangle is a PhysicsObject with a Triange Renderable object.
 * It has 4 constructors:  One takes the size,
 * One takes the size and pickable boolean
 * One takes the the size, red, green, and blue colors
 * and one takes the size, pickable boolean, red, green, and blue colors */
public class PTriangle extends PhysicsObject implements Serializable {
	private static final long serialVersionUID = -8797366738873613992L;

	private static final float[] VERTICES = {(float)(-Math.sqrt(2))/2,(float)(-Math.sqrt(.5))/2, 
		(float)(Math.sqrt(2))/2,(float)(-Math.sqrt(.5))/2, 
		0,(float)(Math.sqrt(.5))/2 };
	
	private Vector2f[] vertexCache;
	private Vector2f[] normalCache;
	
	private class Renderable extends SceneGraphNode implements Serializable {
		private static final long serialVersionUID = 6078060466990650052L;
		
		public Renderable() {
			super(true);
			scale = 1;
		//	this(true);
		}
		
		public Renderable(boolean pickable) {
			super(pickable);
			scale = 1;
		}
		
		public void renderGeometry(GLAutoDrawable drawable) {
			GL2 gl = drawable.getGL().getGL2();	
			gl.glColor3f(red, green, blue);
			gl.glBegin(GL.GL_TRIANGLES);
			for (int i = 0; i < VERTICES.length; i += 2) {
				gl.glVertex2f(VERTICES[i], VERTICES[i+1]);
			}
			gl.glEnd();
		}
	}
	public PTriangle(){
		renderable = new Renderable(true);
		radius = 2*renderable.scale/3;
	}
	public PTriangle(float size) {
		this(size, true);
	}
	
	public PTriangle(float size, boolean pickable) {
		radius = 2*size/3;
		renderable = new Renderable(pickable);
		renderable.scale = size;
	}
	public PTriangle(float size, boolean pickable, float red, float green, float blue) {
		this.radius = 2* size/3;
		renderable = new Renderable(pickable);
		renderable.scale = size;
		renderable.blue = blue;
		renderable.green = green;
		renderable.red = red;
	}
	
	public void clearCaches() {
		vertexCache = null;
		normalCache = null;
	}

	public Vector2f[] getVertices() {	
		if (vertexCache == null) {
			vertexCache = new Vector2f[VERTICES.length / 2];
			Vector2f tmp = new Vector2f();
			for (int i = 0; i < VERTICES.length; i += 2) {
				tmp.x = VERTICES[i] * renderable.scale;
				tmp.y = VERTICES[i+1] * renderable.scale;
				tmp.sumScale(centerOfMass, -1);
				vertexCache[i/2] = new Vector2f();
				vertexCache[i/2].x = (float)(Math.cos(orientation) * tmp.x - Math.sin(orientation) * tmp.y);
				vertexCache[i/2].y = (float)(Math.sin(orientation) * tmp.x + Math.cos(orientation) * tmp.y);
				vertexCache[i/2].sum(centerOfMass);
				vertexCache[i/2].sum(position);
			}
		}
		return vertexCache;
	}
	
	public Vector2f[] getNormals() {
		if (normalCache == null) {
			Vector2f[] vertices = getVertices();
			normalCache = new Vector2f[vertices.length];

			for (int i = 0; i < vertices.length; i++) {
				normalCache[i] = new Vector2f(vertices[(i+1)%vertices.length]);
				normalCache[i].sumScale(vertices[i], -1);
				normalCache[i].normalize();
				float tmp = normalCache[i].x;
				normalCache[i].x = normalCache[i].y;
				normalCache[i].y = -tmp;
			}
		}
		return normalCache;
	}
}
